package han.chensing.latos.maker.windows.util;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import han.chensing.latos.common.level.util.ObjectUtil;
import han.chensing.latos.common.level.util.bind_event.BindEventTypeUtil;
import han.chensing.latos.maker.windows.dialog.base.BaseDialog;

import java.util.function.Function;

public class BatchEventTypesWidgetsAdd {
    BaseDialog baseDialog;
    Function<Integer,Float> xSupplier;
    Function<Integer,Float> ySupplier;
    Function<Integer,Float> widthSupplier;
    Function<Integer,Float> heightSupplier;
    Class<? extends Actor> toFieldClass;
    int bindEventTypeExtra;
    Skin skin;
    BatchWidgetAddFunction batchWidgetAddFunction;
    int i=0;

    private BatchEventTypesWidgetsAdd(
            BaseDialog baseDialog,
            Function<Integer, Float> xSupplier,
            Function<Integer, Float> ySupplier,
            Function<Integer, Float> widthSupplier,
            Function<Integer, Float> heightSupplier,
            Class<? extends Actor> toFieldClass,
            int bindEventTypeExtra,
            Skin skin,
            BatchWidgetAddFunction batchWidgetAddFunction) {
        this.baseDialog = baseDialog;
        this.xSupplier = xSupplier;
        this.ySupplier = ySupplier;
        this.widthSupplier = widthSupplier;
        this.heightSupplier = heightSupplier;
        this.toFieldClass=toFieldClass;
        this.bindEventTypeExtra=bindEventTypeExtra;
        this.skin = skin;
        this.batchWidgetAddFunction = batchWidgetAddFunction;
    }

    public void start(){
        if (ObjectUtil.checkNull(
                xSupplier, ySupplier,
                widthSupplier, heightSupplier,
                skin,
                batchWidgetAddFunction))
            return;
        BindEventTypeUtil.setInto(baseDialog,toFieldClass,bindEventTypeExtra,
                (eventType, field) -> {
                    Float x = xSupplier.apply(i);
                    Float y = ySupplier.apply(i);
                    Float width = widthSupplier.apply(i);
                    Float height = heightSupplier.apply(i);
                    String name = eventType.name();
                    Actor widget = batchWidgetAddFunction.get(
                            i,
                            name,
                            skin
                    );
                    if (widget==null)return null;
                    widget.setBounds(x,y,width,height);
                    baseDialog.addActor(widget);
                    i++;
                    return widget;
                });
    }

    public static BatchEventTypesWidgetsAdd of(
            BaseDialog baseDialog,
            Function<Integer, Float> xSupplier,
            Function<Integer, Float> ySupplier,
            Function<Integer, Float> widthSupplier,
            Function<Integer, Float> heightSupplier,
            Class<? extends Actor> toFieldClass,
            int bindEventTypeExtra,
            Skin skin,
            BatchWidgetAddFunction batchWidgetAddFunction){
        return new BatchEventTypesWidgetsAdd(
                baseDialog,
                xSupplier,
                ySupplier,
                widthSupplier,
                heightSupplier,
                toFieldClass,
                bindEventTypeExtra,
                skin,
                batchWidgetAddFunction);
    }

    @FunctionalInterface
    public interface BatchWidgetAddFunction{
        Actor get(int index, String fieldArg, Skin theSkin);
    }
}
